require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')

require('../lib/iconfont/iconfont.css')

// weui-轻提示
require('weui')
const weui= require('weui.js')

// 确认验证码是否正确
const axios=require('axios')

require('../css/hands.less')

window.addEventListener('DOMContentLoaded',function(){
  let closeBtn=document.querySelector('#closeBtn')
  let mask=document.querySelector('#mask')
  let continueO=document.querySelector('.continueO')
  let over=document.querySelector('.over')
  let fragments=JSON.parse(sessionStorage.getItem('fragments'))
  let videoBtn=document.querySelector('#videoBtn')
  let curName=document.querySelector('#curName')
  let durNmame=document.querySelector('#durNmame')
  let curTips=document.querySelector('#curTips')
  let imgUrl=document.querySelector('#imgUrl')
  let titleTip=document.querySelector('#titleTip')

  let turnLeft=document.querySelector('#turnLeft')
  let turnRight=document.querySelector('#turnRight')
  let progress=document.querySelector('#progress')

  let timeId=null;
  let times=0 //计算时间
  getTime(); //点开页面开始计时
  
  closeBtn.addEventListener('click',function(){
    // alert(1)
    mask.style.display='block'
    videoBtn.pause()
    clearInterval(timeId)
  })

  continueO.addEventListener('click',function(){
    mask.style.display='none'
    videoBtn.play()
    getTime();
  })

  over.addEventListener('click',function(){
    let params={
      "id":sessionStorage.getItem('cid'),
      "userId":localStorage.getItem('userId'),
      "takeTime":times,
      "calorie":times*0.36
    }
    console.log(params.calorie);
    axios.post("http://139.9.177.51:8099/sports/saveTrain",params)
    .then(function(res){
      if(res.data.status===0){
        //成功
        weui.toast(`恭喜你成功消耗${params.calorie.toFixed(2)}卡路里`,{
          callback:function(){
            location.href='./short-part.html'
          }
        })
      }else{
        //失败
        weui.toast("继续努力!")
      }
    })
  })

  let index=0
  Render()
  function Render(){
    videoBtn.src='http://139.9.177.51:8099'+fragments[index].videoUrl
    videoBtn.autoplay=true
    videoBtn.muted=true
    curName.textContent=index+1
    durNmame.textContent=fragments.length
    curTips.textContent=fragments[index].title
    imgUrl.src='http://139.9.177.51:8099'+fragments[index].imgUrl
    titleTip.textContent=fragments[index].title
  }

  turnLeft.addEventListener('click',function(){
    index--
    if(index===-1){
      index=fragments.length-1
    }
    Render()
  })
  turnRight.addEventListener('click',function(){
    index++
    if(index===fragments.length){
      index=0
    }
    Render()
  })

  videoBtn.addEventListener('timeupdate',function(){
    let durTime=this.duration
    let curTime=this.currentTime
    let TimeCompare=curTime/durTime*100
    progress.style.width=TimeCompare+'%'
  })

  videoBtn.addEventListener('ended',function(){
      weui.toast('播放下一个训练课程',{
        duration:2000,
        callback:function(){
          turnRight.click()
        }
      })
  })

  function getTime(){
    timeId=setInterval(function(){
      times++
      //console.log(times);
    },1000)
  }

})